/*
 * Sensor.cpp
 *
 *  Created on: Mar 2, 2012
 *      Author: Zon
 */
#include <string.h>
#include <omnetpp.h>
#include "ArpeesDef.h"
#include "Sensor.h"

class Sensor : public cSimpleModule
{
  protected:
	int lm;
	int xpos,ypos;
	int trRange;
	double energy;
	cModule *nodePtr[NUMBERNODES+4];

    virtual void forwardMessage(cMessage *msg);
    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
    virtual void initNodes();
};

Define_Module(Sensor);

void Sensor::initialize()
{
	this->xpos = par("xpos");
	this->ypos = par("ypos");

	//cModule *parent=this->getParentModule();

	this->trRange=par("trRange");



	//if(getIndex()==5)
	{
	    cMessage *cmsg=new cMessage();
	        cmsg->setKind(SMSG_INIT);
	        scheduleAt(simTime()+1000,cmsg);
	}
	//initNodes();
}

void Sensor::handleMessage(cMessage *msg)
{
    if(msg->isSelfMessage())
    {
        ev<<"Sensor node "<<this->getIndex()<<" got self message type: "<<msg->getKind()<<"\n";
        if(msg->getKind()==SMSG_INIT)
        {
            ev<<"Sensor got start message \n";
            initNodes();
        }
    }
}

void Sensor::forwardMessage(cMessage *msg)
{
    // In this example, we just pick a random gate to send it on.
    // We draw a random number between 0 and the size of gate `out[]'.
    int n = gateSize("out");
    int k = intuniform(0,n-1);
    ev<<"n="<<n;
    ev<<"k="<<k;
    EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
    //send(msg, "out", k);
}

void Sensor::initNodes()
{
    int i;
    cModule *parent=this->getParentModule();
    cModule *mod;
    int numNodes;

    numNodes=(int)parent->par("numNodes");
    trRange=par("trRange");

    for(int i = 2; i <= simulation.getLastModuleId(); i++)
    {
        int x,y,id;

        mod=(cModule *)simulation.getModule(i);
        //if(strcmp(mod->getName(),"node")==0)
        {
            //id=((Sensor *)mod)->id;
            x=((Sensor *)mod)->xpos;
            y=((Sensor *)mod)->ypos;
            //nodePtr[id]=((Sensor *)mod);
            ev<<"ID cua nut "<<this->getId()<<": "<<id;
            if(this->getId()!=mod->getId())
            {
                double xlength=(double)x-this->xpos;
                double ylength=(double)y-this->ypos;
                double distance=sqrt(xlength*xlength+ylength*ylength);
                if(distance<(double)this->trRange)
                {
                    char idStr[2];
                    sprintf(idStr, "%d to %d", this->getId(), i);
                    char gateName[5] = "link";
                    char *temp = strcat(gateName, idStr);
                    cGate *ownGate = addGate(temp, cGate::OUTPUT);
                    cGate *otherGate = mod->addGate(temp, cGate::INPUT);
                    ownGate->connectTo(otherGate);
                    //hide link arow
                    //cDisplayString& connDispStr = ownGate->getDisplayString();
                    //connDispStr.parse("ls=,0");

                }
                }
            }
        }
    }





